#!/bin/bash
set -uo pipefail
#==============================================================#
# File      :   pg-failover-callback
# Desc      :   Patroni event callback script
# Ctime     :   2018-12-06
# Mtime     :   2022-11-11
# Path      :   /pg/bin/pg-failover-callback
# Deps      :   pg-role
# License   :   AGPLv3 @ https://pigsty.io/docs/about/license
# Copyright :   2018-2025  Ruohang Feng / Vonng (rh@vonng.com)
#==============================================================#
PROG_NAME="$(basename $0)"
PROG_DIR="$(cd $(dirname $0) && pwd)"


#--------------------------------------------------------------#
#                             Usage                            #
#--------------------------------------------------------------#
function usage() {
	cat <<-'EOF'
		NAME
			pg-failover-callback   -- Patroni event callback scripts

		SYNOPSIS
			pg-failover-callback <event> <role> <cluster>

		DESCRIPTION
			This is patroni pg event callback scripts, it will be invoked
			by patroni when certain event happens:
			Available events: on_stop|on_start|on_role_change
			Available roles:  master|replica
			<cluster> will be passed as target cluster name

		EXAMPLES
			pg-failover-callback  on_role_change    master  pg-test
			pg-failover-callback  on_start          replica pg-meta


	EOF
	exit 1
}


#--------------------------------------------------------------#
#                             Path                             #
#--------------------------------------------------------------#
LOGPATH=$(grep -A2 'log:' /pg/bin/patroni.yml | head -n3 | awk '/dir:/ {print $2}')


function on_role_change_handler() {
	local role=$1
	local cluster=$2
	local detect_role=$(/pg/bin/pg-role)
	printf "\033[0;32m[$(date "+%Y-%m-%d %H:%M:%S")][${HOSTNAME}][role_change@${cluster}] new_role=${role} detect_role=${detect_role} \033[0m\n" >>$LOGPATH/callback.log

  # perform two checkpoint after failover
  printf "\033[0;32m[$(date "+%Y-%m-%d %H:%M:%S")][${HOSTNAME}] CHECKPOINT \033[0m\n" >>$LOGPATH/callback.log
	psql -Atqwc 'CHECKPOINT;CHECKPOINT;'
}

function on_stop_handler() {
	local role=$1
	local cluster=$2
	exit 0
}

function on_start_handler() {
	local role=$1
	local cluster=$2
	exit 0
}


#--------------------------------------------------------------#
#                             Main                             #
#--------------------------------------------------------------#
function main() {
	local event=$1
	local role=$2
	local cluster=$3

	# unify role to primary & replica
	case ${role} in
	primary | p | master | m | leader | l)
		role="primary"
		patroni_role="primary"
		;;
	standby | s | replica | r | slave)
		role="replica"
		patroni_role="replica"
		;;
	*)
		exit 1
		;;
	esac

	# log event and call handler
	printf "\033[0;32m[$(date "+%Y-%m-%d %H:%M:%S")][${HOSTNAME}][event=${event}}] [cluster=${cluster}] [role=${role}]\033[0m\n" >>$LOGPATH/callback.log
	case ${event} in
	on_stop)
		on_stop_handler ${role} ${cluster}
		;;
	on_start)
		on_start_handler ${role} ${cluster}
		;;
	on_role_change)
		on_role_change_handler ${role} ${cluster}
		;;
	*)
		usage
		;;
	esac
}

main $@
